<?php

namespace app\controllers;

use app\models\Incr;
use app\models\MemberCards;
use app\service\CardService;
use BaseComponents\base\BaseController;

class ApiController extends BaseController
{

    /**
     * 填写卡号报名
     * @param $cardNum
     * @return bool
     */
    public function apiMemberCard($cardNum)
    {
        return (new CardService())->apply($cardNum);
    }

    /**
     * 预热视频播放统计
     */
    public function apiPlayStat()
    {
        Incr::publicityVideoPlayStat();
    }

    /**
     * H5页面浏览次数统计
     */
    public function apiBrowseStat()
    {
        Incr::publicityBrowseStat();
    }

    /**
     * 底部按钮1点击统计
     */
    public function apiJumpLink1Stat()
    {
        Incr::jumpLink1Stat();
    }

    /**
     * 底部按钮2点击统计
     */
    public function apiJumpLink2Stat()
    {
        Incr::jumpLink2Stat();
    }

    /**
     * 点赞统计
     * @param $count
     * @return int
     */
    public function apiLike($count)
    {
        /** @var \Redis $redis */
        $redis = \Yii::$app->get('redis');

        return $redis->incrBy('like', intval($count));
    }

    /**
     * 获取同时在线人数
     * @param $card
     * @return array
     */
    public function apiAudience($card)
    {
        /** @var \Redis $redis */
        $redis = \Yii::$app->get('redis');

        $isMember = MemberCards::isMember($card);

        return array(
            'audience' => $redis->incr('audience'),
            'liveSwitch' => $this->apiLiveSwitch()['liveSwitch'],
            'isMember' => $isMember
        );
    }

    /**
     * 直播开关
     * @return array
     */
    public function apiLiveSwitch()
    {
        return array(
            'liveSwitch' => (new CardService())->liveSwitch()
        );
    }

}